const { mysql } = require('../qcloud')

module.exports = async (ctx) => {
    //接收前端传递的变量 openid, add
    const { openid, add } = ctx.request.body
    try {
        //通过openid来查看用户最后操作记录
        const res = await mysql('records').where('openid', openid).select('mark').orderBy('id', 'desc').first()
        if (res) {
            //查询到最后操作，则当前分数为最后操作分数
            var mark = res.mark
        }else {
            //没有最后操作，用户分数为0
            var mark = 0
        }
        mark = mark + add;
        console.log('分数', mark);

        //将分数插入数据库
        await mysql('records').insert({
            openid, add, mark
        })
        //向前端传递成功信息
        ctx.state.data = {
            code: 0,
            msg: "success"
        }
        console.log('操作成功');
    }catch(e) {
        ctx.state = {
            code: -1,
            data: {
                msg: '添加失败' + e.sqlMessage
            }
        }
    }
    
    
}